﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;

namespace RoadQualityEvalAPI.Helpers
{
    public static class GeneralHelpers
    {

        public struct Point
        {
            public double X { get; set; }
            public double Y { get; set; }

            public Point(double x, double y) : this()
            {

                this.X = x;
                this.Y = y;
            }
        }

        public static DateTime ConvertFromJavaDateStringToDateTime(string date)
        {
            string newDate = date.Remove(0, 4);
            Console.WriteLine(newDate);
            return DateTime.ParseExact(newDate.Replace("EEST", "+3"), "MMM dd HH:mm:ss z yyyy", CultureInfo.InvariantCulture);
        }


        public static double ComputeDistanceBetweenTwoLocationPoints(double latitude1, double longitude1, double latitude2, double longitude2)
        {
            //int EARTH_RADIUS = 6371; // earth radius in km
            double theta = longitude1 - longitude2;
            double dist = Math.Sin(Deg2rad(latitude1)) * Math.Sin(Deg2rad(latitude2)) + Math.Cos(Deg2rad(latitude1)) * Math.Cos(Deg2rad(latitude2)) * Math.Cos(Deg2rad(theta));
            dist = Math.Acos(dist);
            dist = Rad2deg(dist);
            dist = dist * 60 * 1.1515;
            dist = dist * 1.609344;		// convert to km
            dist = dist * 1000;         // convert to meters
            return dist;
        }

        private static double Deg2rad(double deg)
        {
            return (deg * Math.PI / 180.0);
        }

        private static double Rad2deg(double rad)
        {
            return (rad * 180 / Math.PI);
        }

        public static double ConvertFromMpsToKmph(double metersPerSecond)
        {
            return (metersPerSecond * 3600) / 1000;
        }

        public static double DistanceBetweenTwoPoints(Point a, Point b)
        {
            double X_Diff = a.X - b.Y, Y_Diff = a.Y - b.Y;
            return Math.Sqrt((double)X_Diff * X_Diff + (double)Y_Diff * Y_Diff);
        }

        public static bool IsBetween(double x, double a, double b)
        {
            return ((a <= b && x >= a && x <= b)) || (a > b && x <= a && x >= b);
        }

        public static double GetDistance(Point point, Point startPoint, Point endPoint, out Point intersection)
        {
            double a, x, y, R;

            if (startPoint.X != endPoint.X)
            {
                a = (double)(endPoint.Y - startPoint.Y) / (endPoint.X - startPoint.X);
                x = (a * (point.Y - startPoint.Y) + a * a * startPoint.X + point.X) / (a * a + 1);
                y = a * x + startPoint.Y - a * startPoint.X;
            }
            else { x = startPoint.X; y = point.Y; }

            if (IsBetween(x, startPoint.X, endPoint.X) && IsBetween(y, startPoint.Y, endPoint.Y))
            {
                intersection = new Point((double)x, (double)y);
                R = DistanceBetweenTwoPoints(intersection, point);
            }
            else
            {
                double dist1 = DistanceBetweenTwoPoints(point, startPoint), dist2 = DistanceBetweenTwoPoints(point, endPoint);
                if (dist1 < dist2) { intersection = startPoint; R = dist1; }
                else { intersection = endPoint; R = dist2; }
            }

            return R;
        }


    }
}